package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.imagemodule.Free;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.HomeFreeHolder;

import java.util.List;

/** 首页-免费赠送-ListItem */
public class HomeFreeListItem extends DisplayListItem<HomeFreeHolder, Free>
        implements View.OnClickListener {

    private Activity mActivity;
    private Goods mGoods1;
    private Goods mGoods2;
    private Goods mGoods3;
    private String mUrl;

    public HomeFreeListItem(Free s) {
        super(s);
    }

    @Override
    public void onShow(Context context, HomeFreeHolder holder) {
        mActivity = (Activity) context;

        List<Goods> goodsList = data.getGoodsList();
        mUrl = data.getUrl();

        try {

            holder.tvMore.setOnClickListener(this);

            // 第一个商品
            mGoods1 = goodsList.get(0);
            holder.refreshUI1(mGoods1);
            holder.ivImage1.setOnClickListener(this);

            // 第二个商品
            mGoods2 = goodsList.get(1);
            holder.root2.setVisibility(View.VISIBLE);
            holder.refreshUI2(mGoods2);
            holder.ivImage2.setOnClickListener(this);

            // 第三个商品
            mGoods3 = goodsList.get(2);
            holder.root3.setVisibility(View.VISIBLE);
            holder.refreshUI3(mGoods3);
            holder.ivImage3.setOnClickListener(this);

        } catch (IndexOutOfBoundsException e) {
            if (mGoods2 == null) {
                holder.root2.setVisibility(View.INVISIBLE);
            }
            if (mGoods3 == null) {
                holder.root3.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public String getDisplayItemTag() {
        return HomeFreeListItem.class.getSimpleName();
    }

    @Override
    public Class<HomeFreeHolder> getViewHolderClazz() {
        return HomeFreeHolder.class;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_image_1:
                toGoodsDetailActivity(mGoods1);
                break;
            case R.id.iv_image_2:
                toGoodsDetailActivity(mGoods2);
                break;
            case R.id.iv_image_3:
                toGoodsDetailActivity(mGoods3);
                break;
            case R.id.tv_enter:
                toUrlActivity(mUrl);
                break;
        }
    }

    private void toGoodsDetailActivity(Goods goods) {
        GoodsDetailActivity.actionStart(mActivity, goods.getGoodsId());
    }

    private void toUrlActivity(String url) {
        UrlActivity.actionStart(mActivity, url);
    }
}
